package com.example.ytyproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ytyproject.entity.AreaGuizhou;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.mapper.AreaGuizhouMapper;
import com.example.ytyproject.service.AreaGuizhouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-12
 */
@Service
public class AreaGuizhouServiceImpl extends ServiceImpl<AreaGuizhouMapper, AreaGuizhou> implements AreaGuizhouService {

    @Autowired
    private AreaGuizhouMapper areaGuizhouMapper;
    @Autowired
    private CacheManager cacheManager;

    private static final String cachePid = "520000000000";
    private static final String cacheKey = "cacheKey";

    @Override
    public R tree() {
        Cache cache = cacheManager.getCache(cacheKey);
        Cache.ValueWrapper valueWrapper = cache.get(cachePid);
        if (valueWrapper != null){
            Object o = valueWrapper.get();
            return R.ok(o);
        }
        // 1 查询所有的部门
        List<AreaGuizhou> list = this.list();
        // 2 查询最顶级部门
        AreaGuizhou dept = this.getById("520000000000");
        AreaGuizhou trees = getTrees(dept, list);
        cache.put(cachePid, trees);
        return R.ok(trees);
    }

    @Override
    public R listByParentId(String parentId) {
        // 省级地址缓存key
        String cacheKey = "areaGuizhou";
        Cache cache = cacheManager.getCache(cacheKey);
        assert cache != null;
        Cache.ValueWrapper valueWrapper = cache.get(parentId);
        if (valueWrapper != null){
            return R.ok(valueWrapper.get());
        }
        QueryWrapper<AreaGuizhou> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AreaGuizhou::getPid, parentId);
        List<AreaGuizhou> areaGuizhous = areaGuizhouMapper.selectList(queryWrapper);
        cache.put(parentId, areaGuizhous);
        return R.ok(areaGuizhous);
    }

    @Override
    public String getAreaAddress(String areaCode) {
        List<String> list = new ArrayList<>();
        while (true) {
            AreaGuizhou areaGuizhou = areaGuizhouMapper.selectById(areaCode);
            list.add(areaGuizhou.getName());
            if (areaGuizhou.getLevel().equals(1)) {
                break;
            }
            areaCode = areaGuizhou.getPid();
        }
        Collections.reverse(list);
        return String.join("", list);
    }

    /**
     * 递归遍历查询数
     * @param node
     * @param list
     * @return
     */
    public AreaGuizhou getTrees(AreaGuizhou node, List<AreaGuizhou> list) {
        String value = node.getId();
        List<AreaGuizhou> childs = null;
        Iterator<AreaGuizhou> iterator = list.iterator();
        while (iterator.hasNext()) {
            AreaGuizhou m = iterator.next();
            if(m.getPid().equals(value)){
                childs = node.getChildren();
                if(childs == null) {
                    childs = new ArrayList<>();
                }
                childs.add(m);
                iterator.remove();
                node.setChildren(childs);
            }
        }
        if(childs != null && !childs.isEmpty() && !list.isEmpty()){
            for (AreaGuizhou next : childs) {
                getTrees(next, list);
            }
        }
        return node;
    }

}
